/*
 *
 *  Copyright (C) 2009 GSyC/LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */

package com.LibreGeoSocial.App;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

import com.LibreGeoSocial.Api.LibreGeoSocial;
import com.LibreGeoSocial.App.OMF.OMFManager;
import com.LibreGeoSocial.App.Routes.RoutePreferences;

public class LGS_Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {


	public static String KEY_TWITTER_ACTIVE     		= "twitter_active";
	public static String KEY_TWITTER_USER				= "twitter_user";
	public static String KEY_TWITTER_PASSWORD			= "twitter_password";
	public static String KEY_MOVISTAR_ACTIVE    		= "movistar_active";
	public static String KEY_MOVISTAR_USER				= "movistar_user";
	public static String KEY_MOVISTAR_PASSWORD			= "movistar_password";
//	public static String KEY_MOVISTAR_LOCALIZAME_PWD 	= "movistar_localizame_password";
	
	public static String KEY_APP_LOCATION_PRIVACY		= "app_location_privacy";
	public static String KEY_APP_PROFILE_PRIVACY		= "app_profile_privacy";
	public static String KEY_APP_PHOTO_PRIVACY			= "app_photo_privacy";
	public static String KEY_APP_NOTE_PRIVACY			= "app_note_privacy";
	public static String KEY_APP_AUDIO_PRIVACY			= "app_audio_privacy";

	public static String KEY_APP_AUTOLOGIN				= "app_autologin";
	public static final String KEY_APP_PASSWORD 		= "app_password";
	
	
	EditTextPreference userTestPref, userTestPref2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	    setPreferenceScreen(createPreferenceHierarchy());
	      
	    // Set up a listener whenever a key changes            
	    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}

	private PreferenceScreen createPreferenceHierarchy() {
	    // Root
	    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
	      
	    
	    /* CONFIGURACION APPLICATION */
	    PreferenceCategory dialogBasedConfig = new PreferenceCategory(this);
	    dialogBasedConfig.setTitle("Configuration");
	    root.addPreference(dialogBasedConfig);
	    
	    // Autologin preference
	    CheckBoxPreference autologinPref = new CheckBoxPreference(this);
	    autologinPref.setKey(KEY_APP_AUTOLOGIN);
	    autologinPref.setTitle("Auto-login");
	    autologinPref.setSummary("Auto-login activated");
	    dialogBasedConfig.addPreference(autologinPref);
	    
	    // Intent preference
        PreferenceScreen locationPref = getPreferenceManager().createPreferenceScreen(this);
        locationPref.setIntent(new Intent(this,RoutePreferences.class));
        locationPref.setTitle("Location Service");
        locationPref.setSummary("Configure the location settings");
        dialogBasedConfig.addPreference(locationPref);

        
        PreferenceScreen screenPrefPrivacy = getPreferenceManager().createPreferenceScreen(this);
        screenPrefPrivacy.setKey("privacy_preference");
        screenPrefPrivacy.setTitle("Privacy");
        screenPrefPrivacy.setSummary("Set your privacy");
        dialogBasedConfig.addPreference(screenPrefPrivacy);
        
        
        // Perfil privacy
        ListPreference listPrefProfilePrivacy = new ListPreference(this);
        listPrefProfilePrivacy.setEntries(R.array.privacy);
        listPrefProfilePrivacy.setEntryValues(R.array.values_privacy);
        listPrefProfilePrivacy.setDialogTitle("Allow to ...");
        listPrefProfilePrivacy.setKey(KEY_APP_PROFILE_PRIVACY);
        listPrefProfilePrivacy.setTitle("Profile");
        listPrefProfilePrivacy.setSummary("Set privacy about your profile");
        screenPrefPrivacy.addPreference(listPrefProfilePrivacy);
        
        // Location privacy        
        ListPreference listPrefLocationPrivacy = new ListPreference(this);
        listPrefLocationPrivacy.setEntries(R.array.privacy);
        listPrefLocationPrivacy.setEntryValues(R.array.values_privacy);
        listPrefLocationPrivacy.setDialogTitle("Allow to ...");
        listPrefLocationPrivacy.setKey(KEY_APP_LOCATION_PRIVACY);
        listPrefLocationPrivacy.setTitle("Location");
        listPrefLocationPrivacy.setSummary("Set privacy about your position");
        screenPrefPrivacy.addPreference(listPrefLocationPrivacy);
        
        // Photos privacy        
        ListPreference listPrefPhotosPrivacy = new ListPreference(this);
        listPrefPhotosPrivacy.setEntries(R.array.privacy);
        listPrefPhotosPrivacy.setEntryValues(R.array.values_privacy);
        listPrefPhotosPrivacy.setDialogTitle("Allow to ...");
        listPrefPhotosPrivacy.setKey(KEY_APP_PHOTO_PRIVACY);
        listPrefPhotosPrivacy.setTitle("Photos");
        listPrefPhotosPrivacy.setSummary("Set privacy about your photos");
        screenPrefPrivacy.addPreference(listPrefPhotosPrivacy);
        
        // Notes privacy        
        ListPreference listPrefNotesPrivacy = new ListPreference(this);
        listPrefNotesPrivacy.setEntries(R.array.privacy);
        listPrefNotesPrivacy.setEntryValues(R.array.values_privacy);
        listPrefNotesPrivacy.setDialogTitle("Allow to ...");
        listPrefNotesPrivacy.setKey(KEY_APP_NOTE_PRIVACY);
        listPrefNotesPrivacy.setTitle("Notes");
        listPrefNotesPrivacy.setSummary("Set privacy about your notes");
        screenPrefPrivacy.addPreference(listPrefNotesPrivacy);        
	         
        // Audio privacy
        // Notes privacy        
        ListPreference listPrefAudioPrivacy = new ListPreference(this);
        listPrefAudioPrivacy.setEntries(R.array.privacy);
        listPrefAudioPrivacy.setEntryValues(R.array.values_privacy);
        listPrefAudioPrivacy.setDialogTitle("Allow to ...");
        listPrefAudioPrivacy.setKey(KEY_APP_AUDIO_PRIVACY);
        listPrefAudioPrivacy.setTitle("Audio");
        listPrefAudioPrivacy.setSummary("Set privacy about your audios");
        screenPrefPrivacy.addPreference(listPrefAudioPrivacy); 
	    
	    
	    /* OTHER NETWORKS */
	    PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
	    dialogBasedPrefCat.setTitle("Other Networks");
	    root.addPreference(dialogBasedPrefCat);
	        
        PreferenceScreen screenPref = getPreferenceManager().createPreferenceScreen(this);
        screenPref.setKey("twitter_preference");
        screenPref.setTitle("Twitter");
        screenPref.setSummary("Sync yout state with twitter");
        dialogBasedPrefCat.addPreference(screenPref);
        
	    // Manual Location
	    CheckBoxPreference manualPref = new CheckBoxPreference(this);
	    manualPref.setKey(KEY_TWITTER_ACTIVE);
	    manualPref.setTitle("Active Twitter Handler");
	    manualPref.setSummary("Sync your status with twitter");
	    screenPref.addPreference(manualPref);
	     
	    
        // User text   
	    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

		// PROVIDER 
		String username = sharedPreferences.getString(KEY_TWITTER_USER, null); 
		 
		if (username == null)
			username = "<empty>";
			     
        userTestPref = new EditTextPreference(this);
        userTestPref.setDialogTitle("Twitter User");
        userTestPref.setKey(KEY_TWITTER_USER);
        userTestPref.setTitle("Username");
        userTestPref.setSummary(username);
        screenPref.addPreference(userTestPref);
        
        // Pass text
		String password = sharedPreferences.getString(KEY_TWITTER_PASSWORD, null);
		if (password == null)
			password = "<empty>";
		else
			password = "*****";
        
        EditTextPreference passTestPref = new EditTextPreference(this);
        passTestPref.setDialogTitle("Twitter Password");
        passTestPref.setKey(KEY_TWITTER_PASSWORD);
        passTestPref.setTitle("Password");
        passTestPref.setSummary(password);
        screenPref.addPreference(passTestPref);       
        
        
        PreferenceScreen screenPrefMovistar = getPreferenceManager().createPreferenceScreen(this);
        screenPrefMovistar.setKey("movistar_preference");
        screenPrefMovistar.setTitle("Movistar-MovilForum");
        screenPrefMovistar.setSummary("Use movistar services through OpenMovilForum");
        dialogBasedPrefCat.addPreference(screenPrefMovistar);
        
	    
	    // Movistar active
	    CheckBoxPreference movistarPref = new CheckBoxPreference(this);
	    movistarPref.setKey(KEY_MOVISTAR_ACTIVE);
	    movistarPref.setTitle("Active Movistar Handler");
	    movistarPref.setSummary("Allow to use Movistar-OpenMovilForum services");
	    screenPrefMovistar.addPreference(movistarPref);

		// User login 
		username = sharedPreferences.getString(KEY_MOVISTAR_USER, null); 
		 
		if (username == null)
			username = "<empty>";
			     
        userTestPref2 = new EditTextPreference(this);
        userTestPref2.setDialogTitle("Movistar User");
        userTestPref2.setKey(KEY_MOVISTAR_USER);
        userTestPref2.setTitle("Username");
        userTestPref2.setSummary(username);
        userTestPref2.setDialogIcon(R.drawable.combo);
        screenPrefMovistar.addPreference(userTestPref2);
        
        // User pwd
		password = sharedPreferences.getString(KEY_MOVISTAR_PASSWORD, null);
		if (password == null)
			password = "<empty>";
		else
			password = "*****";
        
        passTestPref = new EditTextPreference(this);
        passTestPref.setDialogTitle("Movistar Password");
        passTestPref.setKey(KEY_MOVISTAR_PASSWORD);
        passTestPref.setTitle("Password");
        passTestPref.setSummary(password);
        passTestPref.setDialogIcon(R.drawable.combo);
        screenPrefMovistar.addPreference(passTestPref);    
//        
//        // User Localizame pwd
//		password = sharedPreferences.getString(KEY_MOVISTAR_LOCALIZAME_PWD, null);
//		if (password == null)
//			password = "<empty>";
//		else
//			password = "*****";
//        
//        passTestPref = new EditTextPreference(this);
//        passTestPref.setDialogTitle("Localizame Password");
//        passTestPref.setKey(KEY_MOVISTAR_LOCALIZAME_PWD);
//        passTestPref.setTitle("Localizame Password");
//        passTestPref.setSummary(password);
//        passTestPref.setDialogIcon(R.drawable.combo);
//        screenPrefMovistar.addPreference(passTestPref);   
        
        
        
	    return root;
	        
	}

	public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) 
	{
		
		
		if (key.equals(KEY_TWITTER_USER)) {

			Preference pref = this.findPreference(key);
			if(pref == null) 
				return;
			
			pref.setSummary(sharedPreferences.getString(key, ""));
		}
		
		if (key.equals(KEY_TWITTER_PASSWORD)) {

			Preference pref = this.findPreference(key);
			if(pref == null) 
				return;
			
			pref.setSummary("*****");
		}
		
		if (key.equals(KEY_MOVISTAR_USER)) {

			Preference pref = this.findPreference(key);
			
			if(pref == null) 
				return;
			
			String user = sharedPreferences.getString(key, "");
			OMFManager.setPhone(user);
			pref.setSummary(user);
		}
		
		if (key.equals(KEY_MOVISTAR_PASSWORD)) {

			Preference pref = this.findPreference(key);
			if(pref == null) 
				return;
			
			String pwd = sharedPreferences.getString(key, "");
			OMFManager.setMovPwd(pwd);
			pref.setSummary("*****");
		}
		
//		if (key.equals(KEY_MOVISTAR_LOCALIZAME_PWD)) {
//
//			Preference pref = this.findPreference(key);
//			if(pref == null) 
//				return;
//			
//			String pwd_loc = sharedPreferences.getString(key, "");
//			OMFManager.setMovLocalizamePwd(pwd_loc);
//			pref.setSummary("*****");
//		}
		
		if (key.equals(KEY_APP_LOCATION_PRIVACY))
		{
			String s_privacy = sharedPreferences.getString(key, "");
			
			LibreGeoSocial.getInstance().setPrivacyNode(LibreGeoSocial.getInstance().getInfoUser(false,1).getId(),
														s_privacy,
														true);
			
		}

		if (key.equals(KEY_APP_PROFILE_PRIVACY))
		{
			String s_privacy = sharedPreferences.getString(key, "");
			
			LibreGeoSocial.getInstance().setPrivacyNode(LibreGeoSocial.getInstance().getInfoUser(false,1).getId(),
														s_privacy,
														false);	
			
		}
		
		
	    
	}
}
